package com.marketing.common;

import java.util.List;

/**
 * 分页结果封装类
 * 
 * @param <T> 数据类型
 */
public class PageResult<T> extends Result<List<T>> {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 当前页码
     */
    private Long current;
    
    /**
     * 每页显示条数
     */
    private Long size;
    
    /**
     * 总记录数
     */
    private Long total;
    
    /**
     * 总页数
     */
    private Long pages;
    
    /**
     * 是否有下一页
     */
    private Boolean hasNext;
    
    /**
     * 是否有上一页
     */
    private Boolean hasPrevious;
    
    /**
     * 构造函数
     */
    public PageResult() {
    }
    
    /**
     * 构造函数
     *
     * @param current 当前页码
     * @param size 每页显示条数
     * @param total 总记录数
     * @param pages 总页数
     * @param hasNext 是否有下一页
     * @param hasPrevious 是否有上一页
     */
    public PageResult(Long current, Long size, Long total, Long pages, Boolean hasNext, Boolean hasPrevious) {
        this.current = current;
        this.size = size;
        this.total = total;
        this.pages = pages;
        this.hasNext = hasNext;
        this.hasPrevious = hasPrevious;
    }
    
    // Getter and Setter methods
    public Long getCurrent() {
        return current;
    }
    
    public void setCurrent(Long current) {
        this.current = current;
    }
    
    public Long getSize() {
        return size;
    }
    
    public void setSize(Long size) {
        this.size = size;
    }
    
    public Long getTotal() {
        return total;
    }
    
    public void setTotal(Long total) {
        this.total = total;
    }
    
    public Long getPages() {
        return pages;
    }
    
    public void setPages(Long pages) {
        this.pages = pages;
    }
    
    public Boolean getHasNext() {
        return hasNext;
    }
    
    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }
    
    public Boolean getHasPrevious() {
        return hasPrevious;
    }
    
    public void setHasPrevious(Boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }
    
    /**
     * 成功响应分页数据
     * 
     * @param data 数据列表
     * @param current 当前页
     * @param size 每页大小
     * @param total 总记录数
     * @param <T> 数据类型
     * @return PageResult对象
     */
    public static <T> PageResult<T> success(List<T> data, Long current, Long size, Long total) {
        PageResult<T> result = new PageResult<>();
        result.setCode(200);
        result.setMessage("操作成功");
        result.setData(data);
        result.setCurrent(current);
        result.setSize(size);
        result.setTotal(total);
        
        // 计算总页数
        long pages = (total + size - 1) / size;
        result.setPages(pages);
        
        // 设置分页信息
        result.setHasNext(current < pages);
        result.setHasPrevious(current > 1);
        
        return result;
    }
    
    /**
     * 空分页结果
     * 
     * @param current 当前页
     * @param size 每页大小
     * @param <T> 数据类型
     * @return PageResult对象
     */
    public static <T> PageResult<T> empty(Long current, Long size) {
        return success(List.of(), current, size, 0L);
    }
    
    @Override
    public String toString() {
        return "PageResult{" +
                "current=" + current +
                ", size=" + size +
                ", total=" + total +
                ", pages=" + pages +
                ", hasNext=" + hasNext +
                ", hasPrevious=" + hasPrevious +
                "} " + super.toString();
    }
}